#region File Description
//-----------------------------------------------------------------------------
// GameplayScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RolePlayingGameData;
using System.Collections.Generic;
using System.IO;
using DC2010;
using DC2010.Objects;
using System.Diagnostics;
#endregion

namespace RolePlaying
{
    /// <summary>
    /// This screen implements the actual game logic. It is just a
    /// placeholder to get the idea across: you'll probably want to
    /// put some more interesting gameplay in here!
    /// </summary>
    public class GameplayScreen : GameScreen
    {
        #region Initialization

        /// <summary>
        /// Create a new GameplayScreen object.
        /// </summary>
        public GameplayScreen()
            : base()
        {
            this.Exiting += new EventHandler(GameplayScreen_Exiting);
        }

        /// <summary>
        /// Handle the closing of this screen.
        /// </summary>
        void GameplayScreen_Exiting(object sender, EventArgs e)
        {
            // make sure the session is ending
            // -- EndSession must be re-entrant safe, as the EndSession may be 
            //    making this screen close itself
            Session.EndSession();
        }


        /// <summary>
        /// Load graphics content for the game.
        /// </summary>
        public override void LoadContent()
        {
            Session.StartNewSession(ScreenManager, this);

            ScreenManager.Game.ResetElapsedTime();
        }


        #endregion


        #region Update and Draw


        /// <summary>
        /// Updates the state of the game. This method checks the GameScreen.IsActive
        /// property, so the game will stop updating when the pause menu is active,
        /// or if you tab away to a different application.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (IsActive && !coveredByOtherScreen)
            {
                Session.Update(gameTime);
            }
        }


        /// <summary>
        /// Lets the game respond to player input. Unlike the Update method,
        /// this will only be called when the gameplay screen is active.
        /// </summary>
        public override void HandleInput()
        {
            //CreatureData c = null;
            //if (Root.I.GameSystem.Party.Info.MembersBrowsable.Count > 0) c = Root.I.GameSystem.Party.Info.MembersBrowsable[1];

            List<string> rects = new List<string>();

            //if (Root.I.GameSystem.ItemInHand == null)
            //{
            //    rects = RM.IsPointInRectangle(Root.I.GameSystem.CurrentScreen, InputManager.CurrentMouseState.X-16, InputManager.CurrentMouseState.Y-16);
            // }
            //else
            //{
                rects = RM.IsPointInRectangle(Root.I.GameSystem.CurrentScreen, InputManager.CurrentMouseState.X, InputManager.CurrentMouseState.Y);
            //}

            //mouse over
            if (rects.Count > 0)
            {
                if (InputManager.PreviousMouseState.RightButton == ButtonState.Pressed &&
                    InputManager.CurrentMouseState.RightButton == ButtonState.Released)
                {
                    foreach (string s in rects)
                    {
                        RM.Get(s).Fire_OnMouseRight(InputManager.CurrentMouseState);
                    }
                }

                if (InputManager.PreviousMouseState.LeftButton == ButtonState.Pressed &&
                    InputManager.CurrentMouseState.LeftButton == ButtonState.Released)
                {
                    foreach (string s in rects)
                    {
                        RM.Get(s).Fire_OnMouseLeft(InputManager.CurrentMouseState);
                    }
                }
            }


            if (InputManager.IsActionTriggered(InputManager.Action.MoveForvard))
            {
                Root.I.GameSystem.Party.L.MoveParty(PossibleDirectionsToGo.Forward);

                //Tools.MovePadTexture = "WalkingPad_Up";
                //if (Root.I.MovementSystem.MovePartyUp(Root.I.GameSystem.Party))
                //{
                //    //make it as an event
                //    //Root.I.GameSystem.Party.HitWall();
                //}
                //else
                //{
                //    Tools.IsAlt = !Tools.IsAlt;
                //    Root.I.SoundQueue.AddToQueue("Step");

                //}
            }

            if (InputManager.IsActionTriggered(InputManager.Action.MoveBackwards))
            {
                Root.I.GameSystem.Party.L.MoveParty(PossibleDirectionsToGo.Backward);

                //Tools.MovePadTexture = "WalkingPad_Down";
                //if (Root.I.MovementSystem.MovePartyDown(Root.I.GameSystem.Party))
                //{
                //    //make it as an event
                //    //Root.I.GameSystem.Party.HitWall();
                //}
                //else
                //{
                //    Tools.IsAlt = !Tools.IsAlt;
                //    Root.I.SoundQueue.AddToQueue("Step");

                //}
            }

            if (InputManager.IsActionTriggered(InputManager.Action.MoveLeft))
            {
                Root.I.GameSystem.Party.L.MoveParty(PossibleDirectionsToGo.Left);

                ////Tools.MovePadTexture = "WalkingPad_Left";
                //if (Root.I.MovementSystem.MovePartyLeft(Root.I.GameSystem.Party))
                //{
                //    //make it as an event
                //    //Root.I.GameSystem.Party.HitWall();
                //}
                //else
                //{
                //    Tools.IsAlt = !Tools.IsAlt;
                //    Root.I.SoundQueue.AddToQueue("Step");

                //}
            }

            if (InputManager.IsActionTriggered(InputManager.Action.MoveRight))
            {
                
                Root.I.GameSystem.Party.L.MoveParty(PossibleDirectionsToGo.Right);
                ////Tools.MovePadTexture = "WalkingPad_Right";
                //if (Root.I.MovementSystem.MovePartyRight(Root.I.GameSystem.Party))
                //{
                //    //make it as an event
                //    //Root.I.GameSystem.Party.HitWall();
                //}
                //else
                //{
                //    Tools.IsAlt = !Tools.IsAlt;
                //    Root.I.SoundQueue.AddToQueue("Step");
                //}
            }

            if (InputManager.IsActionTriggered(InputManager.Action.TurnRight))
            {
                
                //Tools.MovePadTexture = "WalkingPad_TurnRight";
                Root.I.GameSystem.Party.L.TurnPartyRight();
                //Root.I.MovementSystem.TurnPartyRight(Root.I.GameSystem.Party);
            }
            if (InputManager.IsActionTriggered(InputManager.Action.TurnLeft))
            {
                
                Root.I.GameSystem.Party.L.TurnPartyLeft();
                //Tools.MovePadTexture = "WalkingPad_TurnLeft";
                //Root.I.MovementSystem.TurnPartyLeft(Root.I.GameSystem.Party);
            }



            //if (InputManager.IsActionTriggered(InputManager.Action.MainMenu))
            //{
            //    ScreenManager.AddScreen(new MainMenuScreen());
            //    return;
            //}

            //if (InputManager.IsActionTriggered(InputManager.Action.ExitGame))
            //{
            //    // add a confirmation message box
            //    const string message =
            //        "Are you sure you want to exit?  All unsaved progress will be lost.";
            //    MessageBoxScreen confirmExitMessageBox = new MessageBoxScreen(message);
            //    confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;
            //    ScreenManager.AddScreen(confirmExitMessageBox);
            //    return;
            //}

        }


        /// <summary>
        /// Event handler for when the user selects Yes 
        /// on the "Are you sure?" message box.
        /// </summary>
        void ConfirmExitMessageBoxAccepted(object sender, EventArgs e)
        {
            ScreenManager.Game.Exit();
        }


        /// <summary>
        /// Draws the gameplay screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            Session.Draw(gameTime);
        }


        #endregion
    }
}
